import requests
import execjs
import db
import daili
import json
import time

url = 'https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token=396c2ee35178d0bbb3af9e6dce53b9e8'

data = {
    'params':'',
    'encSecKey':''
}

headers = {
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
    'referer':'https://music.163.com/',
    'cookie':'_iuqxldmzr_=32; _ntes_nnid=17174b10f6c22864abea6a7c9cb25dd1,1753138737999; _ntes_nuid=17174b10f6c22864abea6a7c9cb25dd1; NMTID=00O8csmZyUqQQ9kJ0u_kNI6FYEYK0YAAAGYLzY02g; WEVNSM=1.0.0; WNMCID=cmcxoi.1753138738345.01.0; WM_TID=mzNLVVh3L5BARRRQQEeGlF2%2BSj1fP1BY; sDeviceId=YD-lZSVJKlW7OpEBhBRFVaWwB3%2FHmgLp61p; ntes_utid=tid._.e%252F%252Bj2zvPZR5BExUEEFfG0A3qGzkP8rho._.0; __snaker__id=FBFlnt9FJIMDpGs1; ntes_kaola_ad=1; gdxidpyhxdE=T%5CzC55tqlaM3wfUdBW3QHT%2FTs%2BSK7vSYs2imjwjw1Q0WHRBLSjEB39%5COKgox6tLqcToK92722ddzur7Kk8%2FRAA8rGPxa06TQU6iUQ4viUOMtorUqgMEuuvJZcigNOXLGN%2FlAjlRcGApqs9shgknd9pONMTEAjDj8WVQC1I8pLUrTp%5C0W%3A1753234014931; P_INFO=13908700156|1753233233|1|music|00&99|null&null&null#yun&530600#10#0|&0||13908700156; __csrf=396c2ee35178d0bbb3af9e6dce53b9e8; MUSIC_U=00560A34FE9B4D07729CBAC5B3AF1B8FF262D32E772823A58BF87599AC264CB966A32D6844FE5046FA3F3CB29E656E1EAA048412E7BB300F50CC2F9EC20DEAF2243CD0D908CF8C8B8B7A6B9A5646DAFECC0C2E1181DCCC8916C0847CA4F18FEE452DF236DE8817C88F7B0391CD55C3CCBCB20C784799821A621E07BB21AE184D0824F08537DECB2EEBD4C4FFAB8291B904B8E231EF4CECBC428CA957D1CDFFDD7ECAE33A06B8A4CC89B4CC887ED966A5ECB9A90BC0032F8CFEC110F0E14ED1BB11FEE6A9C7FBF85FCB0E4ACEB4C40A870660E5F293464139E52B7DC8A3B751DF87C28AC6CD74A48614ACF9C26541B2BD2734062B800E0D6FF859A090B9C408F58D98D078CD388BC8BBD3A11E41656C93CA9D746CA7C1D85D6E821DC0D7D9E2AC95C45523E88B64689309F019375B251EC7954F629E3C925D28031C476AD20FD80539A5E649A0BE90AC19B4931F94E3D635DEDC96E91322EBE594587D52D814B8B0FB34469FC181551989B073CB067B363A6544827B2E77F37F157902F264853250DA28F46618F95CDC30D59807C4E5B4C4; __remember_me=true; WM_NI=bSkUCSC%2BL6Fm5Amy73gZuFxCpLd5%2BSQ33%2B9OvxfgjmJfl2CxIqXFK1bWveICMLyP%2BjGogzKv5YMP40QqOjhH6SRlbCIpSm9HhkTMtBbZlFQG01MSnGkOgLrdq4pmWIsfNjk%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6eeaccf6b9395a1a8f33ff6b88eb7c45e838e8b86d26886ea96a9e13f949799d9f32af0fea7c3b92aa28da5b6c25981f584d7c567f79fa39ab639a586bba7fb49b494acd6aa5993edfdccbc45b6a8aa88e9678abaa4d3c2748baf98a7ca3ba289ba86cc498f94b7b1aa6285b1a99bca7ef78783b1b73c93af818beb44f68d9e8de1498bb09d8ebc44acb59caef74aa286a4a7d44df4ef83b4ed4da991a9adf170b3ecc08ed643b1ba828bea37e2a3; JSESSIONID-WYYY=QU7zIvBidHffnayX89QGOx4T7BxxZfVpM7qOzB%5C9HqixYA9CbTf5FPMnx9GmG3AojoRhP9uin%5Cj9iRXUAFYS9KkuK3k2%2FT0zfhVQyynukgTM5s%2FxNx9O9Ss8umyb%5ChAZ9GRKBrV1XS9g%2FvgASJXWP7daxV2Ka3ZGDkXAE%5CIwRk53WZsS%3A1753333322576'
}

def update_music_info(id,uri):
    con = db.get_con()
    try:
        with con.cursor() as cursor:
            sql = 'update music_163 set is_song=%s,song_link=%s where song_id=%s'
            cursor.execute(sql,(1,uri,id))
            con.commit()
    finally:
        db.close_con(con)

def set_song_link(id):
    with open('music.js', 'r', encoding='utf-8') as f:
        # 读取js文件
        js_core = execjs.compile(f.read())
        # 调用函数。得到结果
        res = js_core.call('get_info', id)
        print(res)
        data['params'] = res['encText']
        data['encSecKey'] = res['encSecKey']
    resp = requests.post(url,data=data,headers=headers)
    dj = json.loads(resp.text)
    uri = dj['data'][0]['url']
    update_music_info(id,uri)

def set_muisc_info(num):
    con = db.get_con()
    try:
        sql = f'select * from music_163 where is_song=0 limit 0,{num}'
        with con.cursor() as cursor:
            cursor.execute(sql)
            rs = cursor.fetchall()
            for d in rs:
                sid = d[1]
                set_song_link(sid)
                time.sleep(3)
                print(sid)

    finally:
        db.close_con(con)

set_muisc_info(3)